package com.stylefeng.guns.modular.pay.controller.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.AjaxRes;
import com.stylefeng.guns.modular.pay.dto.ApiCardParams;
import com.stylefeng.guns.modular.pay.exception.ApiException;
import com.stylefeng.guns.modular.pay.model.BankLimit;
import com.stylefeng.guns.modular.pay.model.Card;
import com.stylefeng.guns.modular.pay.service.IBankLimitService;
import com.stylefeng.guns.modular.pay.service.ICardService;
import com.stylefeng.guns.modular.pay.util.HideDataUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 银行看管理
 * @author Administrator
 *
 */
@Api(value="银行卡绑定",tags="银行卡绑定")
@Controller
@RequestMapping("/api/card")
public class ApiCardController extends BaseController{
    private final static Logger logger = LoggerFactory.getLogger(ApiCardController.class);
    ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private ICardService cardService;
	@Autowired
	private IBankLimitService bankLimitService;
	

	@ApiOperation(value = "绑定卡列表", notes = "绑定卡列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ApiImplicitParam(name = "memberId", value = "会员id", required = true, dataType = "String")
	@ResponseBody
	public AjaxRes list(@RequestParam("memberId")Integer memberId) {
		EntityWrapper<Card> wrapper = new EntityWrapper<Card>();
		wrapper.eq("member_id", memberId);
		wrapper.orderBy("id", true);
		List<Card> cardList = cardService.selectList(wrapper);
		if(cardList==null||cardList.size()==0){
			return new AjaxRes("data.empty","查询不到相关数据");
		}
		for(Card card:cardList){
			card.setCardNo(HideDataUtil.hideCardNo(card.getCardNo()));
		}
		return new AjaxRes(AjaxRes.SUCCESS,"成功", cardList);
	}
	
	
	
	@ApiOperation(value = "银行卡绑定", notes = "银行卡绑定")
	@RequestMapping(value = "/bind", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes bind(ApiCardParams params) {
		try {
			logger.info("请求参数："+mapper.writeValueAsString(params));
			cardService.bind(params);
		} catch (ApiException e) {
			logger.error(e.getRetCode()+"|"+e.getErrMsg());
			return new AjaxRes(e.getRetCode(),e.getErrMsg());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxRes(AjaxRes.SUCCESS, "绑定成功");
	}
	
	
	@ApiOperation(value = "解绑卡", notes = "解绑卡")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String" )
	@ResponseBody
	public AjaxRes delete(@PathVariable("id")Integer id) {
		try {
			cardService.deleteById( id);
		} catch (ApiException e) {
			logger.error(e.getRetCode()+"|"+e.getErrMsg());
			return new AjaxRes(e.getRetCode(),e.getErrMsg());
		}
		return new AjaxRes(AjaxRes.SUCCESS, "绑定成功");
	}
	
	
	@ApiOperation(value = "开户行列表", notes = "开户行列表")
	@RequestMapping(value = "/bankList", method = RequestMethod.GET)
	@ResponseBody
	public AjaxRes bankList() {
		EntityWrapper<BankLimit> wrapper = new EntityWrapper<BankLimit>();
		wrapper.orderBy("id", true);
		List<BankLimit> cardList = bankLimitService.selectList(wrapper);
		if(cardList==null||cardList.size()==0){
			return new AjaxRes("data.empty","查询不到相关数据");
		}
		return new AjaxRes(AjaxRes.SUCCESS,"成功", cardList);
	}
	
	
	
	
	
	
}
